//index.js
import { getNewsData } from '../../utils/api';
import Notify from '../../miniprogram_npm/vant-weapp/notify/notify';
//获取应用实例
const app = getApp();

Page({
	data: {
		loading: false,
		loadingText: '正在加载...',
		currentPage: 1,
		mediaList: [],
		advObj: null
	},
	// 授权获取
	toGetUserInfor() {
		const userInfo = wx.getStorageSync('userInfo')
		const userId = wx.getStorageSync('userId')
		if (userInfo && userId) {
			app.globalData.userInfo = userInfo;
			app.globalData.uid = userId;
		}else{
			// 获取用户信息
			wx.getSetting({
				success: res => {
					if (res.authSetting['scope.userInfo']) {
						// 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
						wx.getUserInfo({
							success: res => {
								// 可以将 res 发送给后台解码出 unionId
								app.globalData.userInfo = res.userInfo
								wx.setStorageSync('userInfo', res.userInfo)
								// 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
								// 所以此处加入 callback 以防止这种情况
								if (app.userInfoReadyCallback) {
									app.userInfoReadyCallback(res)
								}
							},
							fail: () => {
								app.globalData.userInfo = null;
								wx.redirectTo({
									url: `../login/index?page=/pages/newspaperToday/index`
								})
							}
						})
					}
					else{
						wx.redirectTo({
							url: `../login/index?page=/pages/newspaperToday/index`
						})
					}
				}
			})
		}
	},
	onReachBottom() {
		// 下拉触底，先判断是否有请求正在进行中
		if (!this.data.loading) {
		  	this.toGetDataList(this.data.currentPage + 1)
		}
	},
	toGetDataList(currentPage) {
		const params = {
			page: currentPage
		};
		this.setData({
			loading: true,
			loadingText: '正在加载...'
		})
		getNewsData(params).then((res)=>{
			this.setData({
				loading: false
			})
			wx.stopPullDownRefresh();
			if(res.code === 200){
				if(res.data && res.data.length > 0){
          res.data.map(item => {
            item.publish_time = item.publish_time.split(' ')[0]
          })
					this.setData({
						["mediaList[" + currentPage + "]"]: res.data,
						currentPage: currentPage
					})
					if(res.data.length < 15){
						this.setData({
							loading: true,
							loadingText: '没有更多了'
						})
					}
				}else{
					this.setData({
						loading: true,
						loadingText: '没有更多了'
					})
				}
			}else{
				this.errorToast(res.msg,'error')
			}
		}).catch(e=>{
			this.setData({
				loading: false
			})
			this.errorToast('服务异常','error');
		})
	},
	// 跳转详情
	toDetailsPage(event) {
		const id = event.currentTarget.dataset.id;
		wx.navigateTo({
			url: `../webviewPage/index?id=${id}`
		})
	},
	// 错误提示
	errorToast(text, type) {
		switch(type) {
			case 'success':
				Notify({
					text: text,
					color: '#ffffff',
					backgroundColor: '#008000'
				});
				break;
			case 'error':
				Notify({
					text: text,
					color: '#ffffff',
					backgroundColor: '#FF0000'
				});
				break;
			case 'warn':
				Notify({
					text: text,
					color: '#ffffff',
					backgroundColor: '#FFA500'
				});
				break;
		}
	},
	onLoad(res) {
		this.setData({
			advObj: app.globalData.advObj
		})
		this.toGetUserInfor();
		this.toGetDataList(1);
	},
	// 下拉刷新
	onPullDownRefresh() {
		this.setData({
			mediaList: []
		})
		this.toGetDataList(1);
	}
})
